const db = wx.cloud.database();
const app = getApp();
Page({
  data: {
    leixing: 0,
    leixings: ['请选择领取地区','市区','北城','大学城','沂水','郯城','平邑','兰陵','莒南','蒙阴','临沭','费县','沂南'],
    success:'',
    advresses:[ '请选择地区','兰山区通达路与开阳路交汇处往北200米路东（通达路120号）','兰山区沂蒙北路与济南路交汇北500米路西(东方购物广场北邻)','兰山区琅琊王路与聚财三路交汇南100米路西','沂水县振兴路7号（爱美信酒店南邻）','郯城县郯东路与古城路交汇处北300米路东','平邑县蒙山大道与石都大道交汇处石都印象酒店南100米路西','兰陵县中兴路与会宝路交汇处东北角','莒南县隆山路与华鲁街交汇西100米路北汉庭酒店停车场院内二楼','蒙阴县新城路与蒙山路交叉口北50米路东','临沭县中山路与沭新东街交汇南100米路东四楼（文化广场南邻）','费县城区和平路与建设路交汇南200米路西','沂南县历山路与银杏路交汇北50米路东'],
    latitudes:['36.682253','35.682253','34.682253','33.682253'],
    longitudes:['117.120252','116.120252','115.120252','114.120252'],
    latitude: 0,
    longitude: 0,
    markers: [{id: 1,latitude: 36.682253,longitude: 117.120252}]        
  },

  leixingChange: function (e) {
    console.log('leixing发送选择改变，携带值为', e.detail.value)
    this.setData({
      leixing: e.detail.value
    })
  },
  nav: function () {  
    var that = this;
    wx.openLocation({
      latitude:that.data.latitude,	//维度
      longitude: that.data.longitude, //经度
      name: "leixings[that.data.leixing]",	//目的地定位名称
      address: "advresses[that.data.leixing]"	//导航详细地址
    })
  },


  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      page: options.page,
      agent: options.agent,
    })

  },

  getPhoneNumber(e) {
    db.collection('userInfo').where({
      _openid: app.globalData.openid,
    }).get({
      success: res => {
        console.log(res.data.length)
        if (res.data.length == 0) {
          var that = this;
          wx.cloud.callFunction({
            name: 'getMobile',
            data: {
              weRunData: wx.cloud.CloudID(e.detail.cloudID),
            }
          }).then(res => {
            that.setData({
              mobile: res.result,
            })
            console.log(res.result)

            wx.request({ //发送请求
              url: "https://zg99.offcn.com/index/biaodan/register/?actid=37806&callback=?",
              data: {
                  phone: res.result,
                  fenxiao: that.data.area,
                  geneal: that.data.geneal,
                  leixing: that.data.leixings[that.data.leixing]
              }, //接收参数
              success(res) { //返回
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                      jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                      var theJson = JSON.parse(jsonStr);
                  };
                  console.log(theJson)
                  that.setData({
                    success:'success',
                  })
                  
              }
          })
          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
            method: 'POST',
            data: {
              activeId: 30,
              mobile: res.result,
              areaCode: that.data.area,
              agent: that.data.agent,
              pageId: that.data.page
            },
            success: res => {
              console.log(res);
            }
          })
            
          }).catch(err => {
            console.error(err);
          });

        } else {
          wx.showToast({
            title: '请完整填写信息',
            duration: 2000,
            icon: 'none'
          })
        }
      },
    })
  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function () {
    var that = this;
    var sence = wx.getStorageSync('scene')
    return {
      title: '临沂教招锦鲤',
      path: 'pages/ly_tsyy/ly_tsyy?scene=' + sence + '&by_qr=2'
    }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    var that = this;
    var sence = wx.getStorageSync('scene')
    return {
      title: '临沂教招锦鲤',
      path: 'pages/ly_tsyy/ly_tsyy?scene=' + sence + '&by_qr=2'
    }
  }

})